/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MjChatFrame.java
 *
 * Created on 7-nov-2009, 21.42.29
 */
package Messenj;

import Utils.HtmlParser;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import net.sf.jml.MsnContact;
import net.sf.jml.MsnSwitchboard;
import net.sf.jml.message.MsnDatacastMessage;
import net.sf.jml.message.MsnInstantMessage;

/**
 *
 * @author edoardo
 */
public class MjChatFrame extends javax.swing.JFrame {

    HtmlConverter html;
    HtmlParser hp;
    MsnSwitchboard sb;

    /** Creates new form MjChatFrame */
    public MjChatFrame(MsnSwitchboard sb) {
        initComponents();
        this.setSize(400, this.getHeight());
        html = new HtmlConverter();
        hp = new HtmlParser();
        this.jEditorPane1.setText("");
        this.sb = sb;
        this.setDefaultCloseOperation(MjChatFrame.DISPOSE_ON_CLOSE);
        this.jPanel3.setBounds(0, 0, 400, 80);
        this.setBounds(0, 0, 400, 300);
    }

    public void isTyping() {
    }

    public void contactJoined(MsnContact mc) {
    }

    public void contacLeave(MsnContact mc) {
    }

    public void nudgeReceived() {
        int startx = this.getLocationOnScreen().x;
        int starty = this.getLocationOnScreen().y;
        boolean dir = true;
        try {
            new Player("beep3");
        } catch (LineUnavailableException ex) {
            Logger.getLogger(MjChatFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MjChatFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(MjChatFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i = 0; i < 50; i++) {
            if (dir) {
                if (i > 0) {
                    startx += 6;
                    starty += 6;
                } else {
                    startx += 3;
                    starty += 3;
                }
                this.setLocation(startx, starty);
            } else {
                startx -= 6;
                starty -= 6;
                this.setLocation(startx, starty);
            }
            dir = !dir;
            try {
                Thread.sleep(60);
            } catch (InterruptedException ex) {
                Logger.getLogger(MjChatFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.requestFocus();
    }

    Vector messageReceived = new Vector();

    public void add(MsnInstantMessage mim) {
        String s = hp.getParsedMessage(mim);
        messageReceived.add(s);
        String f = "";
        for(int i = 0;i < messageReceived.size(); i++)
        {
            f = f + this.messageReceived.elementAt(i);
        }
        this.jEditorPane1.setText(f.toString());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jPanel5 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMinimumSize(new java.awt.Dimension(178, 200));
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        jSplitPane1.setDividerLocation(310);

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setPreferredSize(jPanel1.size());
        jScrollPane2.setViewportView(jList1);

        jPanel1.add(jScrollPane2);

        jSplitPane1.setRightComponent(jPanel1);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jSplitPane2.setDividerLocation(230);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel3.setMaximumSize(new java.awt.Dimension(500, 80));
        jPanel3.setMinimumSize(new java.awt.Dimension(246, 80));
        jPanel3.setPreferredSize(new java.awt.Dimension(246, 80));
        jPanel3.setLayout(new java.awt.GridLayout(2, 0));

        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        jButton1.setText("Invia");
        jButton1.setPreferredSize(new java.awt.Dimension(27, 27));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton1);

        jButton2.setText("Trillo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton2);

        jPanel6.setLayout(new java.awt.GridLayout(2, 1));

        jToggleButton1.setText("Grassetto");
        jPanel6.add(jToggleButton1);

        jToggleButton2.setText("Sottolineato");
        jPanel6.add(jToggleButton2);

        jPanel4.add(jPanel6);

        jPanel3.add(jPanel4);

        jPanel5.setLayout(new java.awt.GridLayout(1, 0));
        jPanel5.add(jTextField1);

        jPanel3.add(jPanel5);

        jSplitPane2.setBottomComponent(jPanel3);

        jEditorPane1.setBackground(java.awt.Color.white);
        jEditorPane1.setContentType("text/html");
        jEditorPane1.setEditable(false);
        jScrollPane1.setViewportView(jEditorPane1);

        jSplitPane2.setLeftComponent(jScrollPane1);

        jPanel2.add(jSplitPane2);

        jSplitPane1.setLeftComponent(jPanel2);

        getContentPane().add(jSplitPane1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        /*ThreadNotificator tn = new ThreadNotificator("Ciao mondo");
        tn.start();
        this.nudgeReceived();
         * */
        MsnInstantMessage mim = new MsnInstantMessage();
        mim.setDisplayName(sb.getMessenger().getOwner().getEmail().getEmailAddress());
        mim.setContent(this.jTextField1.getText());
        mim.setUnderline(true);
        // invio alla finestra
        this.add(mim);
        // invio all'amico
        this.sb.sendMessage(mim);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        MsnDatacastMessage nudge = new MsnDatacastMessage();
        nudge.setId(1);
        this.sb.sendMessage(nudge);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments++
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    // End of variables declaration//GEN-END:variables
}
